package com.zsqat.eshop.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsqat.eshop.product.entity.BrandEntity;
import com.zsqat.eshop.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zsqat.eshop.product.entity.CategoryBrandRelationEntity;
import com.zsqat.eshop.product.service.CategoryBrandRelationService;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.R;

/**
 * 品牌分类关联
 *
 * @author zsqat
 * @email 491192853@qq.com
 * @date 2020-11-19 16:40:59
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
  @Autowired
  private CategoryBrandRelationService categoryBrandRelationService;

  /**
   * 列表
   */
  @RequestMapping("/list")
  // @RequiresPermissions("product:categorybrandrelation:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = categoryBrandRelationService.queryPage(params);

    return R.ok().put("page", page);
  }

  /**
   * 获取当前品牌关联的所有分类列表列表
   */
  @GetMapping(value = "/category/list")
  //@RequiresPermissions("product:categorybrandrelation:list")
  public R categoryList(@RequestParam Map<String, Object> params, @RequestParam("brandId") Long brandId) {
    List<CategoryBrandRelationEntity> categoryBrandRelationEntity = categoryBrandRelationService.
        list(new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
    return R.ok().put("data", categoryBrandRelationEntity);
  }

  /**
   * /product/categorybrandrelation/brands/list
   * 获取分类关联的品牌,发布商品第一步中下拉分类时，关联找到该分类下的品牌
   */
  @GetMapping(value = "/brands/list")
  public R relationBrandList(@RequestParam(value = "catId", required = true) Long catId) {
    List<BrandEntity> BrandEntities = categoryBrandRelationService.getBrandsByCatId(catId);
    /*List<BrandVo> BrandVos = BrandEntities.stream().map(item -> {
      BrandVo brandVo = new BrandVo();
      brandVo.setBrandId(item.getBrandId());
      brandVo.setBrandName(item.getName());
      return brandVo;
    }).collect(Collectors.toList());*/
    // λ 简写
    List<BrandVo> BrandVos = BrandEntities.stream()
        .map(x -> new BrandVo(x.getBrandId(), x.getName()))
        .collect(Collectors.toList());
    return R.ok().put("data", BrandVos);
  }

  /**
   * 信息
   */
  @RequestMapping("/info/{id}")
  // @RequiresPermissions("product:categorybrandrelation:info")
  public R info(@PathVariable("id") Long id) {
    CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

    return R.ok().put("categoryBrandRelation", categoryBrandRelation);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  // @RequiresPermissions("product:categorybrandrelation:save")
  public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
    // categoryBrandRelationService.save(categoryBrandRelation);
    categoryBrandRelationService.saveDetail(categoryBrandRelation);
    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  // @RequiresPermissions("product:categorybrandrelation:update")
  public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
    categoryBrandRelationService.updateById(categoryBrandRelation);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  // @RequiresPermissions("product:categorybrandrelation:delete")
  public R delete(@RequestBody Long[] ids) {
    categoryBrandRelationService.removeByIds(Arrays.asList(ids));

    return R.ok();
  }

  /**
   * 获取品牌关联的所有分类列表
   */
  @RequestMapping(value = "/catelog/list", method = RequestMethod.GET)
  public R catelogList(@RequestParam("brandId") Long brandId) {
    List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list(
        new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));

    return R.ok().put("data", data);
  }

}
